import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBC {
    public static void main(String[] args) throws SQLException {
        //1.使用DateSource来描述MySQL服务器的位置
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/tmp1?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        //2.和数据库服务器建立连接
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        //使用JDBC操作数据库
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入ID");
        int num = scanner.nextInt();
        System.out.println("请输入名字");
        String name = scanner.next();
        String sql = "insert into student values(?,?,22,33,44)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,num);
        statement.setString(2,name);
        System.out.println(statement);
        //执行SQL语句
        int n = statement.executeUpdate();
        System.out.println(n);
        statement.close();
        connection.close();
    }
}
